package com.anjufang.service.user.impl;

import com.anjufang.entity.SourceHouse;
import com.anjufang.entity.Task;
import com.anjufang.mapper.SourceHouseMapper;
import com.anjufang.service.user.HouseExcelService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class HouseExcelServiceImpl extends ServiceImpl<SourceHouseMapper, SourceHouse> implements HouseExcelService {

    @Autowired
    private SourceHouseMapper sourceHouseMapper;

    private static int SPACE_LINE = 110; // 房屋类型面积分割点

    @Override
    public void importExcel(List<Object[]> dataList) {

        // 清除旧数据
        sourceHouseMapper.deleteAll();

        Task task = new Task();
        task.setTaskType(1);
        task.setCreateTime(new Date());
        task.setUpdateTime(new Date());
        task.insert();

        List<SourceHouse> list = new ArrayList<>();
        for(Object[] obj : dataList) {
            SourceHouse sourceHouse = new SourceHouse();
            sourceHouse.setTaskId(task.getId());
            sourceHouse.setAddress(String.valueOf(obj[0])); // 地址
            sourceHouse.setFloor(String.valueOf(obj[1])); // 楼层
            sourceHouse.setRoomNo(String.valueOf(obj[2])); // 房号
            sourceHouse.setHuxing(String.valueOf(obj[3])); // 户型
            sourceHouse.setHouseType(String.valueOf(obj[4])); // 房屋类型
            sourceHouse.setAreaSpace(String.valueOf(obj[5])); // 建筑面积
            sourceHouse.setRoomSpace(String.valueOf(obj[6])); // 套内面积
            sourceHouse.setRemark(String.valueOf(obj[7])); // 备注
            sourceHouse.setStatus(0); // 默认未分配
            sourceHouse.setCreateTime(new Date());
            sourceHouse.setUpdateTime(sourceHouse.getCreateTime());
            // 设置房屋类型
            if (compare(sourceHouse.getAreaSpace()) < 0) {
                sourceHouse.setType(0);
            } else {
                sourceHouse.setType(1);
            }
            list.add(sourceHouse);
        }
        if (list.size() > 0) {
            super.insertBatch(list);
        }
    }

    private int compare(String area) {
        BigDecimal a = new BigDecimal(area);
        BigDecimal b = new BigDecimal(SPACE_LINE);
        return a.compareTo(b);
    }

}
